const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 查找"日志管理"菜单作为参考排序
    const [logsMenu] = await queryInterface.sequelize.query(
      `SELECT id, order_num FROM menus WHERE name = 'logs' LIMIT 1`,
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    );

    // 检查是否已存在文件管理菜单
    const [existingMenu] = await queryInterface.sequelize.query(
      `SELECT id FROM menus WHERE name = 'file-management' LIMIT 1`,
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    );

    if (existingMenu) {
      console.log('✅ 文件管理菜单已存在');
      return;
    }

    const fileMenuId = uuidv4();
    const orderNum = logsMenu ? logsMenu.order_num + 10 : 70;

    // 创建文件管理菜单
    await queryInterface.bulkInsert('menus', [
      {
        id: fileMenuId,
        parent_id: null,
        name: 'file-management',
        title: '文件管理',
        path: '/files',
        component: '/files/index',
        type: 'menu',
        icon: 'mdi:folder-multiple-outline',
        order_num: orderNum,
        visible: true,
        status: true,
        cache: false,
        created_at: now,
        updated_at: now,
      },
    ]);

    console.log('✅ 文件管理菜单添加成功');
  },

  down: async (queryInterface) => {
    await queryInterface.sequelize.query(
      `DELETE FROM menus WHERE name = 'file-management'`
    );
  },
};

